package de.jmda.fx.node.behaviour;

import javafx.scene.Node;
import javafx.scene.input.MouseEvent;

import org.apache.log4j.Logger;

import de.jmda.fx.node.EventHandler;

public class BehaviourMouseDraggingStart
		extends AbstractNodeBehaviour<MouseEvent>
		implements Behaviour<MouseEvent>
{
	private final static Logger LOGGER =
			Logger.getLogger(BehaviourMouseDraggingStart.class);

	private BehaviourMouseDraggingEvents behaviourMouseDraggingEvents;

	public BehaviourMouseDraggingStart(
			Node node, BehaviourMouseDraggingEvents behaviourMouseDraggingEvents)
  {
	  super(node);
	  
	  this.behaviourMouseDraggingEvents = behaviourMouseDraggingEvents;

	  javafx.event.EventHandler<? super MouseEvent> currentMouseEventHandler =
	  		node.onMousePressedProperty().get();
	  
	  EventHandler<MouseEvent> eventHandler;

	  if (currentMouseEventHandler == null)
	  {
	  	eventHandler = new EventHandler<MouseEvent>();
	  	eventHandler.add(this);
	  	node.onMousePressedProperty().set(eventHandler);
	  }
	  else if (currentMouseEventHandler instanceof EventHandler)
		{
	  	eventHandler = (EventHandler<MouseEvent>) currentMouseEventHandler;
			eventHandler.add(this);
	  	node.onMousePressedProperty().set(eventHandler);
	  }
		else
		{
			LOGGER.error(
					"failure adding behaviour " + getClass().getName() + " to " +
					currentMouseEventHandler.getClass().getName() + " event handler");
		}
  }

	@Override
  public void showBehaviour(MouseEvent mouseEvent)
  {
		behaviourMouseDraggingEvents.setMouseDraggingStart(mouseEvent);
  }
}